//
//  ZLSpringAnimationView.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/11.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "ZLSpringAnimationView.h"

@implementation ZLSpringAnimationView


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CASpringAnimation *spriAnimation = [CASpringAnimation animationWithKeyPath:@"position.x"];
    spriAnimation.fromValue = [NSNumber numberWithFloat:self.layer.position.x];
    spriAnimation.toValue = [NSNumber numberWithFloat:self.layer.position.x + 100];
    spriAnimation.autoreverses = YES;
    spriAnimation.repeatCount = MAXFLOAT;
    
    // 阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快
    spriAnimation.damping = 5;
    // 刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快
    spriAnimation.stiffness = 100;
    // 质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大
    spriAnimation.mass = 1;
    // 初始速率，动画视图的初始速度大小 Defaults to zero
    // 速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    spriAnimation.initialVelocity = 0;
    //估算时间 返回弹簧动画到停止时的估算时间，根据当前的动画参数估算
    spriAnimation.duration = spriAnimation.settlingDuration;
    
    [self.layer addAnimation:spriAnimation forKey:spriAnimation.keyPath];
}


@end
